package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.MigrateLine;
import com.zyd.shiro.business.service.PakMigrateLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakMigrateLine;
import com.zyd.shiro.persistence.mapper.PakMigrateLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakMigrateLineServiceImpl implements PakMigrateLineService {

    @Autowired
    private PakMigrateLineMapper pakMigrateLineMapper;

    @Override
    public MigrateLine insert(MigrateLine migrateLine) {
        Assert.notNull(migrateLine, "对象不可为空！");
        migrateLine.getPakMigrateLine().setUpdateTime(new Date());
        migrateLine.getPakMigrateLine().setCreateTime(new Date());
        pakMigrateLineMapper.insertSelective(migrateLine.getPakMigrateLine());
        return migrateLine;
    }

    @Override
    public void insertList(List<MigrateLine> migrateLines) {
            for(MigrateLine migrateLine:migrateLines){
               migrateLine.getPakMigrateLine().setUpdateTime(new Date());
               migrateLine.getPakMigrateLine().setCreateTime(new Date());
               pakMigrateLineMapper.insertSelective(migrateLine.getPakMigrateLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakMigrateLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(MigrateLine migrateLine) {
        Assert.notNull(migrateLine, "对象不可为空！");
        migrateLine.setUpdateTime(new Date());
        return pakMigrateLineMapper.updateByPrimaryKey(migrateLine.getPakMigrateLine()) > 0;
    }

    @Override
    public boolean updateSelective(MigrateLine migrateLine) {
        Assert.notNull(migrateLine, "对象不可为空！");
        migrateLine.setUpdateTime(new Date());
        return pakMigrateLineMapper.updateByPrimaryKeySelective(migrateLine.getPakMigrateLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public MigrateLine getByPrimaryKey(Long primaryKey) {
        PakMigrateLine pakMigrateLine = pakMigrateLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakMigrateLine ? null : new MigrateLine(pakMigrateLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public MigrateLine getOneByEntity(MigrateLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakMigrateLine pakMigrateLine = pakMigrateLineMapper.selectOne(test.getPakMigrateLine());
        return null == pakMigrateLine ? null : new MigrateLine(pakMigrateLine);
    }

    @Override
    public List<MigrateLine> listAll() {
        List<PakMigrateLine> pakMigrateLines = pakMigrateLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakMigrateLines)) {
            return null;
        }
        List<MigrateLine> migrateLines = new ArrayList<>();
        for (PakMigrateLine pakMigrateLine : pakMigrateLines) {
            migrateLines.add(new MigrateLine(pakMigrateLine));
        }
        return migrateLines;
    }

    @Override
    public List<MigrateLine> listByEntity(MigrateLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<MigrateLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakMigrateLine> pakMigrateLines = pakMigrateLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakMigrateLines)) {
            return null;
        }
        List<MigrateLine> migrateLines = new ArrayList<>();
        for (PakMigrateLine pakMigrateLine : pakMigrateLines) {
            migrateLines.add(new MigrateLine(pakMigrateLine));
        }
        PageInfo bean = new PageInfo<PakMigrateLine>(pakMigrateLines);
        bean.setList(migrateLines);
        return bean;
    }
@Override
    public void increase(PakMigrateLine pakMigrateLine) {
        pakMigrateLineMapper.insert(pakMigrateLine);
    }

    @Override
    public List<PakMigrateLine> findByMigrate(Long migrateId) {
        return pakMigrateLineMapper.findByMigrate(migrateId);
    }


}
